package handlers

import (
	"github.com/gin-gonic/gin"
	"hj4/api/reponset"
	"hj4/api/request"
	"hj4/dao"
	"hj4/models"
)

func Register(c *gin.Context) {
	var data request.Register
	if err := c.ShouldBind(&data); err != nil {
		reponset.ResPonSetDataError(c, err.Error())
		return
	}
	user, err := dao.GetUser(data.Phone)
	if err != nil {
		reponset.ResPonSetDataError(c, "查询失败")
		return
	}
	if user.Phone != "" {
		reponset.ResPonSetDataError(c, "用户已注册")
		return
	}
	users := models.User{
		Phone:    data.Phone,
		Password: data.Password,
	}
	err = dao.Create(&users)
	if err != nil {
		reponset.ResPonSetDataError(c, "添加失败")
		return
	}
	reponset.ResPonSetDataSuccess(c, user.ID)
}
func Login(c *gin.Context) {
	var data request.Register
	if err := c.ShouldBind(&data); err != nil {
		reponset.ResPonSetDataError(c, err.Error())
		return
	}
	user, err := dao.GetUser(data.Phone)
	if err != nil {
		reponset.ResPonSetDataError(c, "查询失败")
		return
	}
	if user.Phone != "" {
		reponset.ResPonSetDataError(c, "用户已注册")
		return
	}
	if user.Password != data.Password {
		reponset.ResPonSetDataError(c, "密码错误")
		return
	}
	reponset.ResPonSetDataSuccess(c, user.ID)
}
